/*
 * 英才汇硕信息科技有限公司 拥有本软件版权 2021 并保留所有权利。
 * Copyright 2021, YCHS Information&Science Techology Co.,Ltd,
 * All right reserved.
 */
package com.ychs.lawyerback.service.impl;

import com.ychs.lawyerback.common.ResultCode;
import com.ychs.lawyerback.dto.SealOptDTO;
import com.ychs.lawyerback.dto.SealQueryDTO;
import com.ychs.lawyerback.exception.BussinessException;
import com.ychs.lawyerback.mapper.SealMapper;
import com.ychs.lawyerback.pojo.SealEntity;
import com.ychs.lawyerback.service.SealService;
import com.ychs.lawyerback.vo.LoginUserVo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mayujing
 * @version 1.0
 */

@Service
public class SealServiceImpl implements SealService {

    @Autowired
    private SealMapper sealMapper;

    @Override
    public Map<String, Object> getSealsByCondition(SealQueryDTO sealQueryDTO) {
        List<SealEntity> seals = sealMapper.getByCondition(sealQueryDTO);
        int total = sealMapper.getCountByCondition(sealQueryDTO);

        Map<String, Object> result = new HashMap<>();
        result.put("list", seals);
        result.put("total", total);
        return result;
    }

    @Override
    @Transactional
    public Long addSeal(SealOptDTO sealOptDTO) {
        //获取当前登录用户
        HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        LoginUserVo loginUserVo = (LoginUserVo) httpServletRequest.getAttribute("loginUserVo");
        Long userId = loginUserVo.getId();

        //DTO转Entity
        SealEntity sealEntity = new SealEntity();
        BeanUtils.copyProperties(sealOptDTO,sealEntity);

        // 设置创建人/更新人信息
        sealEntity.setCreateBy(userId);
        sealEntity.setUpdateBy(userId);
        sealEntity.setCreateTime(new Date());
        sealEntity.setUpdateTime(new Date());

        // 插入数据库
        int rows = sealMapper.insert(sealEntity);
        if (rows <= 0) {
            throw new BussinessException(ResultCode.DATA_INSERT_FAIL);
        }

        return sealEntity.getId();

    }

    @Override
    public int editSeal(SealOptDTO sealOptDTO) {
        SealEntity exist = sealMapper.selectById(sealOptDTO.getId());
        if (exist == null) {
            throw new BussinessException(ResultCode.SEAL_NOT_DOUND);
        }

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        LoginUserVo loginUserVo = (LoginUserVo) request.getAttribute("loginUserVo");
        Long userId = loginUserVo.getId();

        //DTO转Entity
        SealEntity sealEntity = new SealEntity();
        BeanUtils.copyProperties(sealOptDTO,sealEntity);
        sealEntity.setUpdateBy(userId);
        sealEntity.setUpdateTime(new Date());

        //更新数据库
        return sealMapper.update(sealEntity);
    }

    @Override
    @Transactional
    public SealEntity getSealById(Long id) {
        return sealMapper.selectById(id);
    }

}
